'use strict';


const checkType = (obj) => {
    const map = {
            '[object Boolean]': 'boolean',
            '[object Number]': 'number',
            '[object String]': 'string',
            '[object Function]': 'function',
            '[object Array]': 'array',
            '[object Date]': 'date',
            '[object RegExp]': 'regExp',
            '[object Undefined]': 'undefined',
            '[object Null]': 'null',
            '[object Object]': 'object',
            '[object Symbol]': 'symbol',
            '[object Math]': 'math',
            '[object Set]': 'set',
            '[object WeakSet]': 'weakSet',
            '[object Map]': 'map',
            '[object WeakMap]': 'weakMap',
        }
        // if (obj instanceof Element) {
        //     return 'element'
        // }
    const key = Object.prototype.toString.call(obj)
    return map[key]
}


const spinnerStart = (str = '正在加载...', loading = '|/-\\') => {
    var Spinner = require('cli-spinner').Spinner
    var spinner = new Spinner(`${str} %s`)
    spinner.setSpinnerString(loading)
    spinner.start()
    return spinner
}


const sleep = (time = 3000) => {
    return new Promise(resolve => setTimeout(resolve, time))
}

function exec(command, args, options) {
    const win32 = process.platform === 'win32'
    const cmd = win32 ? 'cmd' : command
    const cmdArgs = win32 ? ['/c'].concat(command, args) : args
    return require('child_process').spawn(cmd, cmdArgs, options || {})
}

function execPromise(command, args, options) {
    return new Promise((resolve, reject) => {
        const p = exec(command, args, options)
        p.on('error', error => {
            reject(error)
        })
        p.on('exit', data => {
            resolve(data)
        })
    })
}



module.exports = {
    checkType,
    spinnerStart,
    sleep,
    exec,
    execPromise
};